package com.boc.crm.base.util;

import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
public class RedisProvider {
  private static final Log logger = LogFactory.getLog(RedisProvider.class);
  private static JedisPool jedispool;

  @Autowired
  public RedisProvider(@Value("${redis.ip}") String ip,
	@Value("${redis.port}") int port,
	@Value("${redis.pool.maxActive:64}") int maxActive,
	@Value("${redis.pool.maxIdle:8}") int maxIdle,
	@Value("${redis.pool.maxWait:1000}") long maxWait,
	@Value("${redis.pool.testOnBorrow:true}") boolean testOnBorrow,
	@Value("${redis.pool.testOnReturn:true}") boolean testOnReturn) {
    JedisPoolConfig jedisconfig = new JedisPoolConfig();
    jedisconfig.setMaxActive(maxActive);
    jedisconfig.setMaxIdle(maxIdle);
    jedisconfig.setMaxWait(maxWait);
    jedisconfig.setTestOnBorrow(testOnBorrow);
    jedisconfig.setTestOnReturn(testOnReturn);
    jedispool = new JedisPool(jedisconfig, ip, port);
    logger.info("============== RedisProvider ok [" + ip + ":" + port + "]");
  }

  public static Jedis getJedis() {
    return (Jedis)jedispool.getResource();
  }

  public static void returnJedis(Jedis jedis) {
    jedispool.returnResource(jedis);
  }
  
	public static Set<String> keys(String keys) {
		Jedis jedis = getJedis();
		Set<String> set = jedis.keys(keys);
		return set;
	}

  public static void main(String[] args) {
    jedispool.getClass();
    System.out.println(keys("aaa"));
  }
}